package src.twice;

import javax.swing.*;
import java.awt.*;
import java.io.*;

public class Ftest extends JFrame {

    private static final long serialVersionUID = 1L;
    private JPanel jContentPane = null;
    private JTextArea jTextArea = null;
    private JPanel controlPanel = null;
    private JButton openButton = null;
    private JButton closeButton = null;

    private JButton getOpenButton(){
        if(openButton == null){
            openButton = new JButton();
            openButton.setText("写入文本");
            openButton.addActionListener(new java.awt.event.ActionListener(){
                public void actionPerformed(java.awt.event.ActionEvent e){
                    File file = new File("C:\\Users\\22528\\Desktop","javaTest.txt");
                    try {
                        FileWriter out = new FileWriter(file);
                        String s = jTextArea.getText();
                        out.write(s);
                        out.close();
                    } catch (IOException ex) {
                        throw new RuntimeException(ex);
                    }
                }
            });
        }
        return openButton;
    }

    private JButton getCloseButton(){
        if(closeButton == null){
            closeButton.setText("读取文本");
            closeButton.addActionListener(new java.awt.event.ActionListener(){
                public void actionPerformed(java.awt.event.ActionEvent e){
                    File file = new File("C:\\Users\\22528\\Desktop","javaTest.txt");
                    try {
                        FileReader in = new FileReader(file);
                        char byt[] = new char[1024];
                        int len = in.read(byt);
                        jTextArea.setText(new String(byt,0,len));
                        in.close();
                    } catch (FileNotFoundException ex) {
                        throw new RuntimeException(ex);
                    } catch (IOException ex) {
                        throw new RuntimeException(ex);
                    }
                }
            });
        }
        return closeButton;
    }

    public Ftest(){
        super();
        initialize();
    }

    private void initialize(){
        this.setSize(300,200);
        this.setContentPane(getJContentPane());
        this.setTitle("JFrame");
    }

    private JPanel getJContentPane(){
        if(jContentPane == null){
            jContentPane.setLayout(new BorderLayout());
            //jContentPane.add(getJTextArea(),BorderLayout.CENTER);
            jContentPane.add(getJContentPane(),BorderLayout.SOUTH);
        }
        return jContentPane;
    }

    public static void main(String[] args) {
        Ftest ftest = new Ftest();
        ftest.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ftest.setVisible(true);
    }
}
